﻿using B.S.Meeting.ErrorCode;
using B.S.Meeting.WriteAPI.Applications.Commands.Depts;
using B.S.Meeting.WriteAPI.Applications.Commands.Roles;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Meeting.WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeptController : ControllerBase
    {
        IMediator mediator;
        ILogger<DeptController> logger;

        public DeptController(IMediator mediator, ILogger<DeptController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 添加部门 
        /// </summary>
        /// <param name="deptName">部门名称</param>
        /// <param name="parentId">父级ID</param>
        /// <param name="userId">主管人员</param>
        /// <param name="deptDesc">部门描述</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddDept(string deptName, int? parentId, int userId, string deptDesc)
        {
            try
            {
                var cmd = new AddDeptCommand
                {
                    DeptName=deptName,
                    ParentId=parentId,
                    UserId=userId,
                    DeptDesc=deptDesc
                };
                logger.LogInformation($"添加新部门：{deptName}，部门描述：{deptDesc}");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加部门失败");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "添加部门失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 设置部门状态
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <param name="deptStatus">要设置成的部门状态</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdDeptStatus(int deptId,int deptStatus)
        {
            try
            {
                UpdDeptStatusCommand cmd = new UpdDeptStatusCommand
                {
                    DeptId=deptId,
                    DeptStatus=deptStatus
                };
                var result= await mediator.Send(cmd);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="deptId">要删除的部门ID</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> DelDept(int deptId)
        {
            try
            {
                DelDeptCommand cmd = new DelDeptCommand
                {
                    DeptId = deptId
                };
                var result=await mediator.Send(cmd);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="command">部门更新信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdDept([FromBody] UpdDeptCommand command)
        {
            try
            {
                if (command == null)
                {
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "请求参数不能为空",
                        Data = 0
                    };
                }

                logger.LogInformation($"尝试修改部门上级ID: {command.ParentId}, 名称: {command.DeptName}, 描述: {command.DeptDesc}");
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"修改部门失败: ID {command?.ParentId}");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "修改部门失败",
                    Data = 0
                };
            }
        }
    }
}
